put TBxFile("SFGetFile",wher,"What file:",0,1,"TEXT",0,0) into rply
if IOResult <> noErr THEN
put "IO error on SFGetFile " & IOResult
exit mouseUp
end if
if item 1 of rply is false then exit mouseup
get item 4 of rply
put the value of it into theVRefNo
put item 6 of rply into pfname
put TBxFile("FSOpen",pfname, theVRefNo, 0) into refNo
if IOResult <> noErr THEN
put "IO error on FSOpen " & IOResult
exit mouseUp
end if
put empty into logEOF
put TBxFile("GetEOF",refNo, 0) into logEOF
if IOResult <> noErr THEN
put "IO error on GetEOF " & IOResult
exit mouseUp
end if
put the value of logEOF into Siz
If Siz>29990 Then
put Siz && "Bytes"
answer "The file is too large to show all of it"
end if
put empty into theText
put TBxFile("FSRead",refNo, siz, 0) into theText
if IOResult <> noErr then
put "IO error on Read " & IOResult
exit mouseUp
end if
put TBxFile("FSClose",refNo) into io
IF IOResult <> noErr THEN
put "IO error on Close " & IOResult
exit mouseUp
end if
put TBxFile("FlushVol",NIL, theVRefNo) into io
IF IOResult <> noErr THEN
put "IO error on Flush " & IOResult
exit mouseUp
end if
if Siz>29990 then
put char 1 to 29990 of theText into card field "notes"
put return & "<MORE>" after card field "notes"
else
put theText into card field "notes"
end if
send mouseUp to card field "stats"
end mouseUp
-- part 5 (button)
-- low flags: 00
-- high flags: 8003
-- rect: left=176 top=294 right=316 bottom=265
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 1
-- font id: 0
-- text size: 12
-- style flags: 0
-- line height: 16
-- part name: write to file
----- HyperTalk script -----
on mouseUp
global IOResult, theText
put 0 into NoErr
put -43 into fnfErr
put 1 into fsFromStart
put 100 into item 1 of wher
put 100 into item 2 of wher
put empty into rply
put TBxFile("SFPutFile",wher,"Save Field as:","output",0,0) into rply
if IOResult <> noErr THEN
put "IO error on SFPutFile " & IOResult
exit mouseUp
end if
put rply
if item 1 of rply is false then exit mouseup
get item 4 of rply
put the value of it into theVRefNo
put item 6 of rply into pfname
put TBxFile("FSOpen",pfname, theVRefNo, 0) into refNo
if (IOResult <> noErr) AND (IOResult <> fnfErr) THEN
put "IO error on FSOpen " & IOResult
exit mouseUp
end if
if IOResult=fnfErr then
put TBxFile("Create",pfname, theVRefNo, "????","TEXT") into io
if IOResult <> noErr THEN
put "IO error on Create " & IOResult
exit mouseUp
end if
put TBxFile("FSOpen",pfname, theVRefNo, 0) into refNo
if IOResult <> noErr THEN
put "IO error on FSOpen " & IOResult
exit mouseUp
end if
else
put TBxFile("GetEOF",refNo, 0) into logEOF
if IOResult <> noErr THEN
put "IO error on GetEOF " & IOResult
exit mouseUp
end if
put TBxFile("SetFPos",refNo, fsFromStart, logEOF) into io
if IOResult <> noErr THEN
put "IO error on SetFPos " & IOResult
exit mouseUp
end if
end if
if theText is empty then put card field "notes" into theText
put the length of theText into Siz
put TBxFile("FSWrite",refNo, siz, theText) into io
if IOResult <> noErr then
put "IO error on Write " & IOResult
exit mouseUp
end if
put TBxFile("FSClose",refNo) into io
IF IOResult <> noErr THEN
put "IO error on Close " & IOResult
exit mouseUp
end if
put TBxFile("FlushVol",NIL, theVRefNo) into io
IF IOResult <> noErr THEN
put "IO error on Flush " & IOResult
exit mouseUp
end if
end mouseUp
-- part 6 (field)
-- low flags: 01
-- high flags: 4002
-- rect: left=4 top=316 right=339 bottom=118
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 0
-- font id: 3
-- text size: 9
-- style flags: 0
-- line height: 10
-- part name: stats
----- HyperTalk script -----
on mouseUp
global theText
put empty into card field "stats"
put the length of theText && "characters" into line 1 of card field "stats"
put the number of lines of theText && "lines" into line 2 of card field "stats"
end mouseUp
-- part 8 (button)
-- low flags: 00
-- high flags: 8003
-- rect: left=388 top=319 right=341 bottom=434
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 1
-- font id: 0
-- text size: 12
-- style flags: 0
-- line height: 16
-- part name: Sort
----- HyperTalk script -----
on mouseUp
global theText
if the optionKey is down then
put "S" into hang
else
put empty into hang
end if
answer "Ascending or Descending?" with "Cancel" OR "Descending" OR "Ascending"
if it is "Cancel" then exit mouseUp
if it is "Descending" then
put "D" after hang
else
put "A" after hang
end if
ask "Set Key Column?" with 1
if it is empty then exit mouseUp
if it > 0 then
put it into keyCol
put "K" after hang
else
put 1 into keyCol
end if
answer "Case Sensitive" with "Cancel" OR "Yes" OR "No"
if it is "cancel" then exit mouseUp
if it is "Yes" then put "C" after hang
put theText into thingg
put empty into card field "Notes"
put sorter(thingg,hang,keyCol) into theText
if the length of theText > 29990 then
put char 1 to 29990 of theText into card field "notes"
put return & "<MORE>" after card field "notes"
else
put theText into card field "notes"
end if
send mouseUp to card field "stats"
end mouseUp
-- part 10 (button)
-- low flags: 00
-- high flags: 8003
-- rect: left=166 top=319 right=341 bottom=265
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 1
-- font id: 0
-- text size: 12
-- style flags: 0
-- line height: 16
-- part name: about Sorter...
----- HyperTalk script -----
on mouseUp
hide card field cc
set the scroll of card field algorithms to 0
show card field algorithms
end mouseUp
-- part 13 (button)
-- low flags: 00
-- high flags: 8003
-- rect: left=385 top=294 right=316 bottom=431
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 1
-- font id: 0
-- text size: 12
-- style flags: 0
-- line height: 16
-- part name: case
----- HyperTalk script -----
on mouseUp
global theText
answer "UPPER or lower?" with "Cancel" OR "lower" OR "UPPER"
if it is "Cancel" then exit mouseUp
put "U" into hang
if it is "lower" then put "L" into hang
put theText into thingg
put empty into card field "Notes"
put CaseChg(thingg,hang) into theText
if the length of theText > 29990 then
put char 1 to 29990 of theText into card field "notes"
put return & "<MORE>" after card field "notes"
else
put theText into card field "notes"
end if
send mouseUp to card field "stats"
end mouseUp
-- part 14 (button)
-- low flags: 00
-- high flags: 8003
-- rect: left=431 top=294 right=316 bottom=512
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 1
-- font id: 0
-- text size: 12
-- style flags: 0
-- line height: 16
-- part name: capitalize
----- HyperTalk script -----
on mouseUp
global theText
put "C" into hang
put theText into thingg
put empty into card field "Notes"
put CaseChg(thingg,hang) into theText
if the length of theText > 29990 then
put char 1 to 29990 of theText into card field "notes"
put return & "<MORE>" after card field "notes"
else
put theText into card field "notes"
end if
send mouseUp to card field "stats"
end mouseUp
-- part 15 (button)
-- low flags: 00
-- high flags: 8003
-- rect: left=267 top=319 right=341 bottom=385
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 1
-- font id: 0
-- text size: 12
-- style flags: 0
-- line height: 16
-- part name: about CaseChg...
----- HyperTalk script -----
on mouseUp
hide card field algorithms
set the scroll of card field cc to 0
show card field cc
end mouseUp
-- part 11 (field)
-- low flags: 01
-- high flags: 0007
-- rect: left=8 top=4 right=318 bottom=475
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 0
-- font id: 3
-- text size: 10
-- style flags: 0
-- line height: 12
-- part name: algorithms
----- HyperTalk script -----
on mouseUp
hide card field algorithms
end mouseUp
-- part 16 (field)
-- low flags: 81
-- high flags: 0007
-- rect: left=34 top=4 right=307 bottom=484
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 0
-- font id: 3
-- text size: 10
-- style flags: 0
-- line height: 12
-- part name: cc
----- HyperTalk script -----
on mouseUp
hide card field cc
end mouseUp
-- part 17 (button)
-- low flags: 00
-- high flags: 0002
-- rect: left=122 top=317 right=340 bottom=163
-- title width / last selected line: 0
-- icon id / first selected line: 6720 / 6720
-- text alignment: 1
-- font id: 0
-- text size: 12
-- style flags: 0
-- line height: 16
-- part name: return
----- HyperTalk script -----
on mouseUp
visual effect scroll right
go to card "title page"
end mouseUp
-- part contents for card part 11
----- text -----
Sorter - by Joseph F. Buchanan, University of Utah Version 1.0 2/9/88
Version 1.5 9/28/88
Version 2.0 11/09/88
fixed to work with variables>30K
Version 2.0.1 6/29/90
fixed bug when less than two lines given
This XFCN will take the lines of a field (variable) and sorts it, either ascending or descending OR it will scramble (randomize) it. If the last line is not followed by a carraige return, one will be added. The maximum size for the sort depends upon available memory. Just remember that HyperCard fields are limited to 32000 bytes, variables are not so limited. The first argument to Sorter is the variable(field) to sort, the second is a set of letters specifying the method and/or options:
ΓÇó A - ascending sort
ΓÇó D - descending sort
ΓÇó R - randomize the lines
ΓÇó C - case sensitive (default setting is case insensitive - see Notes)
ΓÇó K - Key column sort - a third argument will specify the column to start sort on
ΓÇó S - Show sort progress (it also slows the progress) - this will show in an info window
ΓÇó V - display the version number of Sorter - this will show in an info window
Notes: Sorter offers two methods of string comparison: Case sensitive and case insensitive. Case sensitive comparison sets the order based entirely on the ASCII codes, i.e. all uppercase letters will sort higher than any lowercase letters. Case insensitive comparison uses the Macintosh ROM Toolbox International Utilities IUMagString function to set the order. This style of ordering treats uppercase and lowercase letters the same unless the entire string is otherwise identical. It also takes into consideration standard diacritical marks. See pages 501 - 503 of Inside Macintosh, Volume 1 from Addison-Wesley for a detailed explanation. Note also that limited testing shows case sensitive sorting is about 3 times faster than case insensitive. Caution: watch out for tabs - they sort to the top.
This particular stack also contains my TBxFile (for doing file IO in Read and Write buttons).
I use a sort algorithm found in "Programming Pearls", pages 245-250 of the
"Communications of the ACM", Volume 28, Number 3, March 1985. I implemented it in assembly language. I also used my own random number generator ( the toolbox RANDOM kept crashing on me) using the formula (found in Knuths book):
X := (aX+c) MOD m
where X is the previous random number (or seed, I used GetDateTime), a=25413,
c=25413, m=32768. I then put the random number into the proper range using the formula:
y:=(x-x0)*(y1-y0)/(x1-x0)+y0 { floating point arithmetic }
where [x0,x1] is the range of my random numbers [0,32767] and [y0,y1] is the range of lines to be sorted [0,nlines]; x is the original random number; y is the one I used.
-- part contents for card part 16
----- text -----
CaseChg - by Joseph F. Buchanan, University of Utah Version 1.0 2/10/88
Version 2.0 11/10/88
fixed to work with variables>30K
This XFCN will take the contents of a field (variable) and convert all lower-case letters to upper-case (option: "U" or none); OR convert all upper-case letters to lower (option: "L" ); OR will capitalize the text (option: "C").
The call is as follows:
put CaseChg(theText,option) into theChanged
OR
put CaseChg(theText) into theChanged
The capitalize option looks for the first letter following a space or special character and converts it to upper-case if it isn't upper already. The special characters include:
! " # $ % & ( ) * + , - . and / also carraige-return and tab and any unprintable
characters in the ASCII range below 20Hex
For best results in capitalization, first convert all to lower, then capitalize.